import {NativeModules,AsyncStorage } from 'react-native';


let storage={

};

export default {
    firstUpperCase:function(str){
        return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
    },

    //封装fetch请求，增加超时配置
    fetch:function(url,options){
        let opt = options || {};
        return Promise.race([
            fetch(url,...opt),
            new Promise(function(resolve,reject){
                setTimeout(()=>reject(new Error('requset timeout')),opt.timeout || 20*1000)
            })
        ])
    },
    serializeJson:function(json){
        let result=''
        for(let item in json){
            result=result+'&'+item+'='+json[item]
        }
        return result.substr(1)
    },
    
    setItem:function(key,value){
        AsyncStorage.setItem('@IFOPStorage:'+key, value);
    },
    getItem:function(key){
        return  storage['@IFOPStorage:'+key];
    },
    setData:(data,executeFunc)=>{
        AsyncStorage.getAllKeys((err, keys) => {
            AsyncStorage.multiGet(keys, (err, stores) => {
                stores.map((result, i, store) => {
                    // get at each store's key/value so you can work with it
                    let key = store[i][0];
                    let value = store[i][1];
                    storage[key]=value

                });

                if(executeFunc){
                    executeFunc(data)
                }
            });
        });
    }

}